﻿using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Sustainalytics.Controversies.Service.Exceptions;
using Sustainalytics.Utils;
using Sustainalytics.Utils.WebApi.ErrorCodes;
using MapOptions = Sustainalytics.Controversies.Service.Util.MapOptions;
using System.Web.Http.Cors;
using Sustainalytics.Controversies.Service.BlInvoker;

namespace Sustainalytics.Controversies.Service.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    //[ClaimsChecker("api/v1/company/controversies")]
    [RoutePrefix("api/v1/company/{companyId}")] //todo stelian:  how about  "api/v1/company{companyId}/controversyIndicator", does it work ? I followed the route strategy used in other services.

    public class IncidentController: ApiController
    {
        private readonly IServiceBlInvoker _blInvoker;
        public IncidentController(IServiceBlInvoker blInvoker)
        {
            _blInvoker = blInvoker;
        }


        [Route("controversyIndicator/eventIndicator/incident")] 
        public HttpResponseMessage Get(Guid companyId, [FromUri] MapOptions mapOptions)
        {
            try
            {
                return Request.CreateResponse(HttpStatusCode.OK,
                    _blInvoker.GetIncidentsMapLocationForCompany(companyId, mapOptions));
            }
            catch (CorporateDataNotFoundException ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, new
                {
                    ErrorStatusCode = WebAPIGeneralErrorCodes.ResourceNotFound,
                    Message = ex.Message
                });
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError,
                    new { ErrorStatusCode = WebAPIGeneralErrorCodes.UnhandledException });
            }
        }

        [Route("controversyIndicator/{controversyIndicatorId}/eventIndicator/{eventIndicatorId}/incident")]
        public HttpResponseMessage Get(Guid companyId, Guid controversyIndicatorId, Guid eventIndicatorId, [FromUri] MapOptions map,[FromUri] ListOptions options=null)
        {
            try
            {
                if (map!=null && map.Location)
                {
                    return Request.CreateResponse(HttpStatusCode.OK,
                    _blInvoker.GetIncidentsMapLocationByEventIndicator(companyId, controversyIndicatorId, eventIndicatorId, map));
                }
                return Request.CreateResponse(HttpStatusCode.OK,
                    _blInvoker.GetIncidentsData(companyId, eventIndicatorId, controversyIndicatorId, options));
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError,
                    new { ErrorStatusCode = WebAPIGeneralErrorCodes.UnhandledException });
            }
        }

        [Route("controversyIndicator/{controversyIndicatorId}/eventIndicator/{eventIndicatorId}/incident/{incidentId}")]
        public HttpResponseMessage Get(Guid companyId, Guid controversyIndicatorId, Guid eventIndicatorId, Guid incidentId)
        {
            try
            {
                return Request.CreateResponse(HttpStatusCode.OK,
                    _blInvoker.GetIncidentData(companyId, controversyIndicatorId, eventIndicatorId, incidentId));
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError,
                    new { ErrorStatusCode = WebAPIGeneralErrorCodes.UnhandledException });
            }
        }

    }
}
